package com.dscs.browser;

import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.openapi.wm.ex.ToolWindowManagerEx;
import com.intellij.openapi.wm.ex.ToolWindowManagerListener;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.io.File;

/**
 * Created by Jamling on 2017/7/11.
 */
public class IMWindowFactory implements ToolWindowFactory {

    public static final String TOOL_WINDOW_ID = "idea-browser";

    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        toolWindow.setToHideOnEmptyContent(true);

        NativeInterface.open();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createContents(project, toolWindow);
            }
        });
        NativeInterface.runEventPump();
        ToolWindowManager manager = ToolWindowManager.getInstance(project);
        if (manager instanceof ToolWindowManagerEx) {
            ToolWindowManagerEx managerEx = ((ToolWindowManagerEx) manager);
            managerEx.addToolWindowManagerListener(new ToolWindowManagerListener() {
                @Override
                public void toolWindowRegistered(@NotNull String id) {
                }

                @Override
                public void stateChanged() {
                    ToolWindow window = ToolWindowManager.getInstance(project).getToolWindow(IMWindowFactory.TOOL_WINDOW_ID);
                    if (window != null) {
                        boolean visible = window.isVisible();
                        if (visible && toolWindow.getContentManager().getContentCount() == 0) {

                            NativeInterface.open();
                            SwingUtilities.invokeLater(new Runnable() {
                                public void run() {
                                    createContents(project, window);
                                }
                            });
                            NativeInterface.runEventPump();
                        }
                    }
                }
            });
        }
    }

    private void createContents(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
        JWebBrowser webBrowser = new JWebBrowser();
        webBrowser.navigate("https://www.baidu.com");
        webBrowser.setButtonBarVisible(false);
//        webBrowser.setMenuBarVisible(false);
//        webBrowser.setBarsVisible(true);
//        webBrowser.setStatusBarVisible(true);
        webBrowser.setLocationBarVisible(true);
        Content content = contentFactory.createContent(webBrowser, "idea-browser", false);
        toolWindow.getContentManager().addContent(content, 0);
    }

}
